﻿@using TimerTask.Model.Common
@using TimerTask.Service
@model Pager<TimerTask.Model.Vms.JobInfoVm>
<div class="row">
    <div class="col-sm-12">
        <table class="table table-striped table-bordered table-hover">
            <thead>
                <tr>
                    <th>Id</th>
                    <th>任务名</th>
                    <th>组名</th>
                    <th>创建时间</th>
                    <th>更新时间</th>
                    <th>联系人邮箱</th>
                    <th>联系人电话</th>
                    <th>执行周期</th>
                    <th>上次执行时间</th>
                    <th>理论下次执行时间</th>
                    <th>当前</th>
                    <th>操作</th>
                </tr>
            </thead>
            <tbody>
                @foreach (var item in Model.Data)
            {
                    <tr>
                        <td>@item.Id</td>
                        <td>@item.JobName</td>
                        <td>@item.GroupName</td>
                        <td>@item.CreateTime.ToString("yyyy-MM-dd HH:mm:ss")</td>
                        <td>@item.UpdateTime.ToString("yyyy-MM-dd HH:mm:ss")</td>
                        <td>@item.ContactEmail</td>
                        <td>@item.ContactTel</td>
                        <td>@item.Cron</td>
                        <td>@item.LastFireTime.ToString("yyyy-MM-dd HH:mm:ss")</td>
                        <td>
                            @if (item.JobStatus == JobStatus.Normal)
                {
                                @QuartzService.GetNextValidTime(item.Cron);
                }
                        </td>
                        <td>
                            @switch (item.JobStatus)
                {
                    case JobStatus.Normal:
                                    @: <span class="glyphicon glyphicon-expand" title="开启"></span>
                        break;
                    case JobStatus.Stop:
                                    <span class="glyphicon glyphicon-unchecked" title="停止"></span>
                                    break;
                                case JobStatus.Del:
                                    <span class="glyphicon glyphicon-remove-circle" title="删除"></span>
                                    break;
                            }
                        </td>
                        <td>
                            @switch (item.JobStatus)
                {
                    case JobStatus.Normal:
                                    <span class="glyphicon glyphicon-pause" title="暂停" onclick="stopJob(@item.Id)"></span>
                                    break;
                                case JobStatus.Stop:
                                    <span class="glyphicon glyphicon-play" title="开启" onclick="startJob(@item.Id)"></span>
                                    <span class="glyphicon glyphicon-chevron-right" title="单次执行" onclick="triggerJob(@item.Id)"></span>
                                    break;
                                case JobStatus.Del:
                                    break;
                            }
                        </td>
                    </tr>
                }
            </tbody>
        </table>
    </div>
</div>
    @Html.Partial("pager", Model)
    <script>
        function startJob(jobId) {
            $.post('@Url.Action("Start")?jobId=' + jobId,
                function (res) {
                    if (res.code !== 1) {
                        alert(res.msg);
                    } else {
                        loadPage(@Model.Index);
                    }
                });
        }

        function stopJob(jobId) {
            $.post('@Url.Action("Stop")?jobId=' + jobId,
                function (res) {
                    if (res.code !== 1) {
                        alert(res.msg);
                    } else {
                        loadPage(@Model.Index);
                    }
                });
        }

        function triggerJob(jobId) {
            $.post('@Url.Action("Trigger")?jobId=' + jobId,
                function (res) {
                    if (res.code !== 1) {
                        alert(res.msg);
                    } else {
                        loadPage(@Model.Index);
                    }
                });
        }
    </script>
